define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'orders/lists/index' + location.search,
                    //add_url: 'orders/lists/add',
                    //edit_url: 'orders/lists/edit',
                    //del_url: 'orders/lists/del',
                    multi_url: 'orders/lists/multi',
                    table: 'orders',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_num', title: __('Order_num')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        {field: 'total_money', title: __('Total_money'), operate:'BETWEEN'},
                        {field: 'payment_time', title: __('Payment_time'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'pay_style', title: __('Pay_style'), searchList: {"1":__('Pay_style 1'),"2":__('Pay_style 2'),"3":__('Pay_style 3')}, formatter: Table.api.formatter.normal},
                        {field: 'names', title: __('Names')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'address', title: __('Address')},
                        {field: 'citys', title: __('省市区')},
                        //{field: 'province', title: __('Province')},
                        //{field: 'city', title: __('City')},
                        //{field: 'area', title: __('Area')},
                        {field: 'logistics', title: __('Logistics')},
                        {field: 'logistics_code', title: __('Logistics_code')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'recharge', title: '', table: table, buttons: [
                                {
                                    name: 'recharge',
                                    text: '订单详情',
                                    icon: 'fa fa-flash',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'orders/detail/index',
                                }
                            ],events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                        {field: 'fahuo', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'fahuo',
                                    text: '发货',
                                    icon: 'fa fa-flash',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'orders/lists/fahuo',
                                }
                            ],events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                if (row.status != 3){
                                    return '';
                                }
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    Form.api.bindevent($("form[role=form]"));
    return Controller;
});